package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.factory.UserFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    /**
     * 测试获取Bean对象的三种方式
     */
    @Test
    public void test1(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        //从IOC容器中获取Bean对象的第一种方式：强制类型转换
        User u=(User) ac.getBean("u");
        u.eat();
        //从IOC容器中获取Bean对象的第二种方式：通过字节码类型，如果IOC容器中有多个相同对象类型时，此时就不知道应该取谁
        User u1 = ac.getBean(User.class);
        u1.eat();
        //第三种方式
        User u2 = ac.getBean("u", User.class);
        u2.eat();
    }


    @Test
    public void test2(){
       ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        User u1 = ac.getBean("u1", User.class);
        u1.eat();
    }

    @Test
    public void test3(){
        /*采用注入方式后使用普通方式创建对象无法获得注入的属性值*/
      /*  User u1=new User();
        System.out.println(u1.getAddress().getCname());*/

        ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        User u = ac.getBean("u", User.class);
        System.out.println(u.getName()+u.getAge());
        System.out.println(u.getBirthday());
        System.out.println(u.getAddress().getCname());
    }
}
